package org.util.config;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;
/**
 * 工具类
 * checkPermissions             检测应用是否存在某一个权限
 * currentNoteworkTypeIsWIFI    检查当前是否使用wifi通讯
 * isWiFiActive					检测wifi当前是否可用
 * isNetworkAvailable			测试当前网络是否可用
 * getTime						获取当前日期		
 * getAppKey					获取在AndroidManifest.xml中的自定义数据
 * getActivityName				获取当前正在运行的顶层Activity
 * getPackageName
 * getOsVersion					或系统版本
 * getDeviceID					获取设备ID
 * checkPhoneState              检测应用是否有phone _state权限
 * getSdkVersion				系统版本
 * getVersionName				versionName
 * getVersionCode				versionCode
 * getCellInfo					获取基站数据
 * getLatitudeAndLongitude		获取位置信息
 * isHaveGravity				判断是否有重力感应
 * getNetworkType				网络类型WIFI or MOBILE
 * isNetworkTypeWifi			判断当前是否使用的wifi
 * 								如果选择了wifi但wifi不可用也返回false
 * getNetworkTypeWIFI2G3G		判断当前是用的wifi 2G 3G
 * @author 梁前武
 * QQ:1587790525
 * www.apkplug.com
 */
public class CommonUtil {
	/**
	 * 检测应用是否存在某一个权限
	 * checkPermissions
	 * @param context
	 * @param permission  
	 * @return true or  false
	 */
	public static boolean checkPermissions(Context context, String permission) {
		PackageManager localPackageManager = context.getPackageManager();
		return localPackageManager.checkPermission(permission, context
				.getPackageName()) == PackageManager.PERMISSION_GRANTED;
	}
	
	/**
	 * 检查当前是否使用wifi通讯
	 * Determine the current networking is WIFI
	 * @param context
	 * @return
	 */
	public  static boolean currentNoteworkTypeIsWIFI(Context context){
		ConnectivityManager connectionManager = (ConnectivityManager)context.
                getSystemService(Context.CONNECTIVITY_SERVICE);   
		return	connectionManager.getActiveNetworkInfo().getType()==ConnectivityManager.TYPE_WIFI;
	}
	/**
	 * 检测wifi当前是否可用
	 * Judge wifi is available
	 * @param inContext
	 * @return
	 */
	    public static boolean isWiFiActive(Context inContext) { 
	    	if(checkPermissions(inContext, "android.permission.ACCESS_WIFI_STATE")){
	    		 Context context = inContext.getApplicationContext();  
	    	        ConnectivityManager connectivity = (ConnectivityManager) context  
	    	                .getSystemService(Context.CONNECTIVITY_SERVICE);  
	    	        if (connectivity != null) {  
	    	            NetworkInfo[] info = connectivity.getAllNetworkInfo();  
	    	            if (info != null) {  
	    	                for (int i = 0; i < info.length; i++) {  
	    	                    if (info[i].getTypeName().equals("WIFI") && info[i].isConnected()) {  
	    	                        return true;  
	    	                    }  
	    	                }  
	    	            }  
	    	        }  
	    	        return false;
	    	}else{
	    		if(Constants.DebugMode){
	    			Log.e("lost permission", "lost--->android.permission.ACCESS_WIFI_STATE");
	    		}
	    		
	    		return false;
	    	}
	    } 
	    
	    /**
		 * Testing equipment networking and networking WIFI
		 * 测试当前网络是否可用
		 * @param context
		 * @return true or false 
		 */
		public static boolean isNetworkAvailable(Context context) {
			if(checkPermissions(context, "android.permission.INTERNET")){
				ConnectivityManager cManager=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); 
				NetworkInfo info = cManager.getActiveNetworkInfo(); 
					if (info != null && info.isAvailable()){     
				        return true; 
				  }else{ 
					  if(Constants.DebugMode){
						  Log.e("error", "Network error");
					  }
				        return false; 
				  } 
			}else{
				if(Constants.DebugMode){
					Log.e(" lost  permission", "lost----> android.permission.INTERNET");
				}
				return false;
			}
		}
		
		/**
		 * Get the current time     format  yyyy-MM-dd HH:mm:ss
		 * 获取当前日期
		 * @return
		 */
		public static String getTime() {
			Date date = new Date();
			SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			return localSimpleDateFormat.format(date);
		}
		
		/**
		 * get   APPKEY
		 * 获取在AndroidManifest.xml中的自定义数据
		 * @param context
		 * @return  appkey
		 */
		public static String getAppKey(Context paramContext) {
		    if(paramContext==null){
		        return "";
		    }
			String umsAppkey;
			try {
				PackageManager localPackageManager = paramContext
						.getPackageManager();
				ApplicationInfo localApplicationInfo = localPackageManager
						.getApplicationInfo(paramContext.getPackageName(), 128);
				if (localApplicationInfo != null) {
					String str = localApplicationInfo.metaData
							.getString("MY_APPKEY");
					if (str != null) {
						umsAppkey = str;
						return umsAppkey.toString();
					}
					if (Constants.DebugMode)
						Log.e("apkplug","Could not read MY_APPKEY meta-data from AndroidManifest.xml.");
				}
			} catch (Exception localException) {
				if (Constants.DebugMode) {
					Log.e("apkplug","Could not read MY_APPKEY meta-data from AndroidManifest.xml.");
					localException.printStackTrace();
				}
			}
			return "";
		}
		
		/**
		 * 获取当前正在运行的顶层Activity
		 * get currnet activity's name
		 * @param context
		 * @return
		 */
		public static String getActivityName(Context context) {
		    if(context==null){
		        return "";
		    }
			ActivityManager am = (ActivityManager) context
					.getSystemService(Context.ACTIVITY_SERVICE);
			if(checkPermissions(context, "android.permission.GET_TASKS")){
				ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
				return cn.getShortClassName();
			}else{
				if(Constants.DebugMode){
					Log.e("lost permission", "android.permission.GET_TASKS");
				}
				
				return "";
			}
			
			
		}
		
		/**
		 * 
		 * get  PackageName
		 * @param context
		 * @return
		 */
		public static String getPackageName(Context context) {
			ActivityManager am = (ActivityManager) context
					.getSystemService(Context.ACTIVITY_SERVICE);
			
			if(checkPermissions(context, "android.permission.GET_TASKS")){
				ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
				return cn.getPackageName();
			}else{
				if(Constants.DebugMode){
					Log.e("lost permission", "android.permission.GET_TASKS");
				}
				
				return null;
			}
			
		}
		
		/**
		 * get  OS number
		 * @param context
		 * @return
		 */
		public static String getOsVersion(Context context) {
			String osVersion = "";
			if (checkPhoneState(context)) {
				osVersion = android.os.Build.VERSION.RELEASE;
				if(Constants.DebugMode){
					Log.d("android_osVersion", "OsVerson" + osVersion);
				}
				
				return osVersion;
			} else {
				if(Constants.DebugMode){
					Log.e("android_osVersion", "OsVerson get failed");
				}
				
				return null;
			}
		}
		
		/**
		 * get deviceid
		 * @param context
		 *            add  <uses-permission android:name="READ_PHONE_STATE" /> 
		 * @return
		 */
		public static String getDeviceID(Context context) {
		    if(context==null){
		        return "";
		    }
			if(checkPermissions(context, "android.permission.READ_PHONE_STATE")){
				String deviceId = "";
				if (checkPhoneState(context)) {
					TelephonyManager tm = (TelephonyManager) context
							.getSystemService(Context.TELEPHONY_SERVICE);
					deviceId = tm.getDeviceId();
				}
				if (deviceId != null) {
					if(Constants.DebugMode){
						Log.d("commonUtil", "deviceId:" + deviceId);
					}
					
					return deviceId;
				} else {
					if(Constants.DebugMode){
						Log.e("commonUtil", "deviceId is null");
					}
					
					return "";
				}
			}else{
				if(Constants.DebugMode){
					Log.e("lost permissioin", "lost----->android.permission.READ_PHONE_STATE");
				}
				
				return "";
			}
		}
		
		/**
		 * check phone _state is readied ;
		 * 
		 * @param context
		 * @return
		 */
		public static boolean checkPhoneState(Context context) {
			PackageManager packageManager = context.getPackageManager();
			if (packageManager.checkPermission("android.permission.READ_PHONE_STATE", context
					.getPackageName()) != 0) {
				return false;
			}
			return true;
		}
		
		/**
		 * get sdk number
		 * @param paramContext
		 * @return
		 */
		public static String getSdkVersion(Context paramContext) {
			String osVersion = "";
			if (!checkPhoneState(paramContext)) {
				osVersion = android.os.Build.VERSION.RELEASE;
				if(Constants.DebugMode){
					Log.e("android_osVersion", "OsVerson" + osVersion);
				}
				
				return osVersion;
			} else {
				if(Constants.DebugMode){
					Log.e("android_osVersion", "OsVerson get failed");
				}
				
				return null;
			}
		}
		
		/**
		 * versionName
		 * Get the version number of the current program
		 * @param context
		 * @return
		 */

		public static String getVersionName(Context context) {
			String curversion = "";
			try {
				// ---get the package info---
				PackageManager pm = context.getPackageManager();
				PackageInfo pi = pm
						.getPackageInfo(context.getPackageName(), 0);
				curversion = pi.versionName;
				if (curversion == null || curversion.length() <= 0) {
					return "";
				}
			} catch (Exception e) {
				if(Constants.DebugMode){
					Log.e("VersionInfo", "Exception", e);
				}
				
			}
			return curversion;
		}
		
		/**
		 * versionCode
		 * Get the version number of the current program
		 * @param context
		 * @return
		 */

		public static int getVersionCode(Context context) {
			
			try {
				// ---get the package info---
				PackageManager pm = context.getPackageManager();
				PackageInfo pi = pm
						.getPackageInfo(context.getPackageName(), 0);
				return pi.versionCode;
			} catch (Exception e) {
				if(Constants.DebugMode){
					Log.e("VersionInfo", "Exception", e);
				}
				
			}
			return 0;
		}
		
		/**
		 * 获取基站数据
	     * Get the base station information
	     * @throws Exception
	     */
	    public static SCell getCellInfo(Context context) throws Exception {
	        SCell cell = new SCell();  
	        TelephonyManager mTelNet = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
	        GsmCellLocation location = (GsmCellLocation) mTelNet.getCellLocation();
	        if (location == null){
	        	if(Constants.DebugMode){
	        		Log.e("GsmCellLocation Error", "GsmCellLocation is null");
	        	}
	        	return null;
	        }
	            
	     
	        String operator = mTelNet.getNetworkOperator();
//	        System.out.println("operator------>"+operator.toString());
	        int mcc = Integer.parseInt(operator.substring(0, 3));
	        int mnc = Integer.parseInt(operator.substring(3));
	        int cid = location.getCid();
	        int lac = location.getLac();
	     
	        cell.MCC=mcc;
	        cell.MCCMNC = Integer.parseInt(operator);
	        cell.MNC = mnc;
	        cell.LAC = lac;
	        cell.CID = cid;
	     
	        return cell;
	    }
	    /**
	     * 获取位置信息
	     * @param context
	     * @param mUseLocationService
	     * @return
	     */
	    public static LatitudeAndLongitude getLatitudeAndLongitude(Context context,boolean mUseLocationService){
	    	LatitudeAndLongitude latitudeAndLongitude  = new  LatitudeAndLongitude();
	    	if(mUseLocationService){
	    		LocationManager loctionManager =(LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
	        	List<String> matchingProviders=  loctionManager.getAllProviders();
	            for(String prociderString :matchingProviders){
//	            	 Log.d("provider",prociderString);
	            	 System.out.println(prociderString);
	            	Location location = loctionManager.getLastKnownLocation(prociderString);
	            	if(location!=null){
//	            		Log.d("ss", location.getLatitude()+"");
	            		latitudeAndLongitude.latitude = location.getLatitude()+"";
	            		latitudeAndLongitude.longitude = location.getLongitude()+"";
	                }else{
	                	latitudeAndLongitude.latitude = "";
	            		latitudeAndLongitude.longitude = "";
	                }
	            	}
	    	}else{
	        	latitudeAndLongitude.latitude = "";
	    		latitudeAndLongitude.longitude = "";
	        }
	    	
	    	
	        return latitudeAndLongitude;
	    	
	    }
	    
	    /**
	     * 判断是否有重力感应
	     * To determine whether it contains a gyroscope
	     * @return
	     */
	  public static boolean isHaveGravity(Context context){
		  SensorManager manager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
		  if(manager==null){
			  return false;
		  }
		return true;
	  }
	  
	  /**
	   * Get the current networking
	   * @param context
	   * @return  WIFI or MOBILE
	   */
	  public static String getNetworkType(Context context){
	      TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
	    int type=  manager.getNetworkType();
	    String typeString="UNKNOWN";
	    if(type==TelephonyManager.NETWORK_TYPE_CDMA){
	    	typeString ="CDMA";
	    }
	    if(type==TelephonyManager.NETWORK_TYPE_EDGE){
	    	typeString ="EDGE";
	    }
	    if(type==TelephonyManager.NETWORK_TYPE_EVDO_0){
	    	typeString ="EVDO_0";
	    }
	    if(type==TelephonyManager.NETWORK_TYPE_EVDO_A){
	    	typeString ="EVDO_A";
	    }
	    if(type==TelephonyManager.NETWORK_TYPE_GPRS){
	    	typeString ="GPRS";
	    }
	    if(type==TelephonyManager.NETWORK_TYPE_HSDPA){
	    	typeString ="HSDPA";
	    }
	    if(type==TelephonyManager.NETWORK_TYPE_HSPA){
	    	typeString ="HSPA";
	    }
	    if(type==TelephonyManager.NETWORK_TYPE_HSUPA){
	    	typeString ="HSUPA";
	    }
	    if(type==TelephonyManager.NETWORK_TYPE_UMTS){
	    	typeString ="UMTS";
	    }
	    if(type==TelephonyManager.NETWORK_TYPE_UNKNOWN){
	    	typeString ="UNKNOWN";
	    }
	    if(type==TelephonyManager.NETWORK_TYPE_1xRTT){
	        typeString ="1xRTT";
	    }
	    if(type==11){
	        typeString ="iDen";
	    }
	    if(type==12){
	        typeString ="EVDO_B";
	    }
	    if(type==13){
	        typeString ="LTE";
	    }
	    if(type==14){
	        typeString ="eHRPD";
	    }
	    if(type==15){
	        typeString ="HSPA+";
	    }
	    
	    
		return typeString;
	  }
	  
	  /**
	   * 判断当前是否使用的wifi
	   * 如果选择了wifi但wifi不可用也返回false
	   * Determine the current network type  
	   * @param context
	   * @return
	   */
	public static boolean isNetworkTypeWifi(Context context) {
		// TODO Auto-generated method stub
		

		if(checkPermissions(context, "android.permission.INTERNET")){
			ConnectivityManager cManager=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); 
			NetworkInfo info = cManager.getActiveNetworkInfo(); 
			
				if (info != null && info.isAvailable()&&info.getTypeName().equals("WIFI")){ 
			        return true; 
			  }else{ 
				  if(Constants.DebugMode){
					  Log.e("error", "Network not wifi");
				  }
			        return false; 
			  } 
		}else{
			if(Constants.DebugMode){
				Log.e(" lost  permission", "lost----> android.permission.INTERNET");
			}
			return false;
		}
	}
	/**
	 * 判断当前是用的wifi 2G 3G
	 * @param context
	 * @return
	 */
	public static String getNetworkTypeWIFI2G3G(Context context){
	      ConnectivityManager cm= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
	      
	      NetworkInfo info= cm.getActiveNetworkInfo();
	      String type =info.getTypeName().toLowerCase();
	      if(type.equals("wifi")){
	          
	      }else{
	          type = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getExtraInfo();  
	          System.out.println(type);
	      }
	      return type;
	      
	  }

	
}
